<?php
namespace app\manage\controller;
class ChannelMaterial extends Base
{
    //列表
    public function index(){
        if( request()->isAjax() ){
            $page = input('page');
            $limit = input('limit');
            $gameId = input('game_id');

            $where['status'] = ['<>', -1];
            !empty($gameId) && $where['game_id'] = $gameId;

            if( ! $count=model('GameMaterial')->where($where)->count() ){
                $this->layTable();
            }

            $select = model('GameMaterial')
                ->where($where)
                ->order('add_time desc')
                ->limit(($page-1)*$limit, $limit)
                ->select();

            $data = [];

            $gameIdName = model('Game')->gameIdName();
            foreach ($select as $k=>$v){
                $data[] = [
                    'id' => $v['id'],
                    'game_id' => $v['game_id'],
                    'game_name' => isset($gameIdName[ $v['game_id'] ]) ? $gameIdName[ $v['game_id'] ] : '',
                    'add_time' => $v['add_time'],
                    'url' => $v['url'],
                    'file_name' => $v['file_name'],
                    'status' => $v['status'],
                ];
            }
            $this->layTable(0, '游戏素材列表', $data, $count);
        }else{
            return view();
        }
    }

    //添加
    public function add(){
        if( request()->isAjax() ){
            $file = request()->file('file');
            $gameId = input('server.http_game');

            empty($gameId) && $this->layJson(1, '请先选择游戏');

            $originPath = ROOT_PATH.'/uploads/material/';
            $saveName = 'material_' . $gameId;

            //上传文件，验证后缀跟文件大小，同名覆盖
            $info = $file
                ->validate(['size'=>50000000, 'ext'=>'zip,rar']) //50M
                ->move($originPath, $saveName);
            if(!$info){
                $this->layJson(1, $file->getError());
            }

            $fileName = $info->getSaveName();
            $filePath = $originPath . $fileName;
            $fileKun = 'channel_material/' . $fileName;

            //上传到金山云
            $url = action('KsyunUpload/put', ['miyu-game-img-bucket', $filePath, $fileKun]);
            !$url && $this->layJson(1, '上传到金山云失败');

            //如果已经存在则直接成功
            $where['game_id'] = $gameId;
            $where['status'] = 1;
            if(model('GameMaterial')->where($where)->count()){
                $this->layJson(0, '上传成功');
            }else{
                $save['game_id'] = $gameId;
                $save['file_name'] = $fileName;
                $save['status'] = 1;
                $save['url'] = $url;
                $addRs = model('GameMaterial')->save( $save );
                $addRs ? $this->layJson(0, '上传成功') : $this->layJson(1, model('GameMaterial')->getError());
            }
        }else{
            return view();
        }
    }

    //添加VIP表
    public function vip(){
        if( request()->isAjax() ){
            $file = request()->file('file');

            $originPath = ROOT_PATH.'/uploads/material/';
            $saveName = date('Y-m-d').'miyuhaiwai-vip';

            //上传文件，验证后缀跟文件大小，同名覆盖
            $info = $file
                ->validate(['size'=>10000000, 'ext'=>'xlsx,xls']) //10M
                ->move($originPath, $saveName);
            if(!$info){
                $this->layJson(1, $file->getError());
            }

            $fileName = $info->getSaveName();
            $filePath = $originPath . $fileName;
            $fileKun = 'channel_material/' . $fileName;

            //上传到金山云
            $url = action('KsyunUpload/put', ['miyu-game-img-bucket', $filePath, $fileKun]);
            !$url && $this->layJson(1, '上传到金山云失败');

            //如果已经存在则直接成功
            if( model('GameMaterial')->where('type', 2)->count()){
                $save['url'] = $url;
                $editRs = model('GameMaterial')->save( $save, ['type'=>2] );
            }else{
                $save['url'] = $url;
                $save['type'] = 2;
                $save['status'] = 1;
                $save['file_name'] = $fileName;
                $editRs = model('GameMaterial')->save( $save );
            }

            $editRs ? $this->layJson(0, '上传成功') : $this->layJson(1, model('GameMaterial')->getError());
        }else{
            $vipUrl = model('GameMaterial')->where('type', 2)->value('url');
            return view('', ['vip_url'=>$vipUrl]);
        }
    }

    //编辑
    public function edit(){
        if( request()->isAjax() ){
            $editRs = model('GameMaterial')->allowField(true)->isUpdate(true)->save( input() );
            $editRs!==false ? $this->layJson(0, '编辑成功') : $this->layJson(1, '编辑失败，请重试');
        }else{
            return view();
        }
    }

    //删除
    public function del(){
//        $fileKun = 'channel_material/' . input('file_name');
//        $rs = action('KsyunUpload/delete', ['miyu-game-img-bucket', $fileKun]);

        $delRs = model('GameMaterial')->allowField(true)->isUpdate(true)->save(['status'=>-1], ['id'=>input('id')]);
        $delRs ? $this->layJson(0, '删除成功') : $this->layJson(1, '删除失败，请重试');
    }
}